package com.investigation.protect;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class MainActivity extends Activity {
    SharedPreferences sp;
    private static final String[] VIDEO_PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.MANAGE_EXTERNAL_STORAGE};
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this,"守护程序已运行",Toast.LENGTH_LONG).show();
        if(! NotificationManagerCompat.from(this).areNotificationsEnabled()){
            Toast.makeText(this,"请打开消息通知",Toast.LENGTH_LONG).show();
            Intent intent=new Intent().setAction("android.settings.APPLICATION_DETAILS_SETTINGS").setData(Uri.fromParts("package", "com.investigation.protect", null));
            startActivity(intent);
        }
//        startService(new Intent(this, protectService.class));
        if (!isIgnoringBatteryOptimizations()){
//            没有在白名单中则
            Toast.makeText(this,"请把protect守护程序加入白名单",Toast.LENGTH_LONG).show();
            requestIgnoreBatteryOptimizations();
        }
        PackageManager pm = getPackageManager();
        boolean permission1 = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", getPackageName()));
        boolean permission2 = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", getPackageName()));
        if(!permission1 || !permission2){
            requestPermission();
        }else {
            finishActivity();
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void requestIgnoreBatteryOptimizations() {
        try {
            @SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(MainActivity.this,VIDEO_PERMISSIONS,1);
        }
        finishActivity();
    }
    public void finishActivity(){
        Intent intent=new Intent();
        intent.setAction("广播2");
        intent.setComponent(new ComponentName(getPackageName(),getPackageName()+".MyReceiver"));
        sendBroadcast(intent);
        finish();
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean isIgnoringBatteryOptimizations() {
        boolean isIgnoring = false;
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
        }
        return isIgnoring;
    }
}